package leetcode_201_300;

public class LeeCode_289 {
    public static void main(String[] args) {
        gameOfLife(new int[][]{
                {0,1,0},
                {0,0,1},
                {1,1,1},
                {0,0,0}
        });
    }
    private static void gameOfLife(int[][] board) {
        int m = board.length;
        int n = board[0].length;
        int[][] ans = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == 1){
                    if (i - 1 >= 0){
                        if (j - 1 >= 0) {
                            ans[i - 1][j - 1]++;
                        }
                        ans[i - 1][j]++;
                        if (j + 1 < n) {
                            ans[i - 1][j + 1]++;
                        }
                    }
                    if (i + 1 < m){
                        if (j - 1 >= 0) {
                            ans[i + 1][j - 1]++;
                        }
                        ans[i + 1][j]++;
                        if (j + 1 < n) {
                            ans[i + 1][j + 1]++;
                        }
                    }
                    if (j + 1 < n)
                        ans[i][j + 1]++;
                    if (j - 1 >= 0)
                        ans[i][j - 1]++;
                }
            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == 0 && ans[i][j] == 3){
                    board[i][j] = 1;
                }
                if (board[i][j] == 1 && (ans[i][j] < 2 || ans[i][j] > 3)){
                    board[i][j] = 0;
                }
            }
        }

    }
}
